package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 得到待派送的订单数量
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Long getStatusNumber(Integer status);

    @Select("select * from order_detail where order_id like concat('%', #{orderId}, '%')")
    List<OrderDetail> getByOrderId(Long orderId);

    /**
     * 根据id获取订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 查询小于指定时间的订单
     * @param status
     * @return
     */
    //@Select("select * from orders where status = #{status} and order_time <= DATE_SUB(CURRENT_TIMESTAMP(), interval 15 minute)")
    @Select("select * from orders where status = #{status} and order_time <= #{time}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    Integer validCountByMap(Map map);

    @Select("select COUNT(*) from orders where order_time BETWEEN #{beginDate} AND #{endDate}")
    Integer countByTime(LocalDateTime beginDate, LocalDateTime endDate);

    Integer countByMap(Map map);

    /**
     * 统计销售量前十菜品
     * @param begin
     * @param end
     * @return
     */
    //XML文件中起的别名要与实体类中字段保持一致
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
}
